<?php

namespace App\Model;

use App\Model\BaseModel;
class CommentModel extends BaseModel
{
    //
    protected $table = 'comment';
    public $timestamps = false;
    public $guarded = [];

//    关联学校外键
    public function school(){
        return $this->hasMany(SchoolModel::class,'id','sid');
    }

    //与自己关联
    public function common(){
        return $this ->hasMany(CommentModel::class,'id','rid');
    }

    public function CardCommentALLUser($cid,$uuid)
    {
       $res =  CommentModel::where(['cid' => $cid,'rid' => 0]) -> orderBy('created_at','desc') ->  get();

       $data = $res;
       foreach ($res as $key => $value){
           $data[$key]['id']  = $value ->id;
           $data[$key]['img']  = $value ->img;
           $data[$key]['name']  = $value ->name;
           $data[$key]['content']  = $value ->content;
           $data[$key]['centent_image']  = $value ->centent_image;
           $data[$key]['like'] =  UserLikeModel::where(['uid' => $uuid,'common_id' => $value ->id]) ->take(1) ->get();
           $comment = CommentModel::where(['rid' => $value -> id]) -> orderBy('created_at','desc') -> get();
           $commentc = CommentModel::where(['rid' => $value -> id]) -> orderBy('created_at','desc')->take(2) -> get();
           $data[$key]['comment']  = $commentc-> makeHidden(['updated_at']);
           $data[$key]['comment_count']  = count($comment);

       }

        if($res){//成功！
            return ['code' => true,'data' => $data -> makeHidden(['updated_at'])];
        }else{
            return ['code' => false,'data' => '数据为空'];
        }
    }




    public function CardCommentDesc($cid)
    {
      $res =   CommentModel::where(['id' => $cid]) ->get();
        $res -> makeHiddel(['updated_at']);
        if($res){//成功！
            return ['code' => true,'data' => $res -> makeHidden(['updated_at'])];
        }else{
            return ['code' => false,'data' => '数据为空'];
        }
    }

    public function CardCommentDescCommentDesc($cid,$uuid)
    {
        //$cid 1
        $res = CommentModel:: get();//10W
        $res -> makeHidden(['updated_at']);
          $data =   $this -> get_attr($res,0,$uuid);
            $result = [];
          foreach ($data as $key => $values){
              if($values['id'] == $cid){
                $result = $values;
              }

          }
//        $res = CommentModel::find($cid);//1
//        $result = $this -> getComment($res);
        if($result){//成功！
            return ['code' => true,'data' => $result ];
        }else{
            return ['code' => false,'data' => '数据为空'];
        }
    }

    //递归
    public function get_attr($a, $pid,$uuid)
    {
        $tree = array();                                //每次都声明一个新数组用来放子元素
        foreach ($a as $v) {
            if ($v['rid'] == $pid ) {                      //匹配子记录
                $v['children'] = $this->get_attr($a, $v['id'],$uuid); //递归获取子记录
                if ($v['children'] == null) {
                    $v['children'] = 0;            //如果子元素为空则unset()
                }
                $v['like'] = UserLikeModel::where(['common_id' => $v ->id,'uid' => $uuid]) ->get();
                $tree[] = $v;                           //将记录存入新数组
            }
        }
        return $tree;                                  //返回新数组
    }

    public function schoolPostMsg($sid)
    {

        //count
        $res =  SchoolModel::find($sid);
        if($res){//成功！
            $res['count'] =  $res -> total_card;

            return ['code' => true,'data' => $res];
        }else{
            return ['code' => false,'data' => '数据为空'];
        }

    }

    public function schoolCommentNum($sid)
    {

        $res['count'] =  CommentModel::where(['sid' => $sid ]) ->count();

        if($res){//成功！
            return ['code' => true,'data' => $res  ];
        }else{
            return ['code' => false,'data' => '数据为空'];
        }

    }

    public function schoolFlowNum($sid)
    {
        $res['count'] =  FlowModel::where(['sid' => $sid]) ->count();

        if($res){//成功！
            return ['code' => true,'data' => $res  ];
        }else{
            return ['code' => false,'data' => '数据为空'];
        }
    }

    public function getComment($uuid, $toid)
    {
        if(is_null($toid) || $toid == 'undefined'){

            $res =  CommentModel::
            with([
                'school' => function($query){
                    $query ->select(['id','school_name']);
                },
                'common' =>function($query){
                }
            ]) ->
            where(['uid' => $uuid]) ->where('rid' ,'!=',0) ->get();


            if($res){//成功！
                return ['code' => true,'data' => $res ];
            }else{
                return ['code' => false,'data' => '数据为空'];
            }

        }else{

            $res =  CommentModel::
            with([
                'school' => function($query){
                    $query ->select(['id','school_name']);
                },
                'common' =>function($query){

                }
            ]) ->
            where(['uid' => $toid]) ->where('rid' ,'!=',0) ->get();

            if($res){//成功！
                return ['code' => true,'data' => $res ];
            }else{
                return ['code' => false,'data' => '数据为空'];
            }
        }

    }

    public function CardComment($cid, $sid, $name, $img, $rid, $centent_image, $val, $uuid)
    {

//       插入数据库
           $res =  CommentModel::create([
                'uid' => $uuid,
                'cid' => $cid,
                'sid' => $sid,
                'img' => $img,
                'name' => $name,
                'rid' => $rid,
                'content' => $val,
                'centent_image' => $centent_image,
                'created_at' => date('Y-m-d H:i:s'),
            ]);

        if($res){//成功！
            return ['code' => true,'data' => $res ];
        }else{
            return ['code' => false,'data' => '保存失败！'];
        }

    }


}
